const errorType = require('../constants/error-type');

const errorHandle = (error, ctx) => {
  let status, message;

  switch(error.message) {
    case errorType.NAME_OR_PASSWORD_NOT_EMPTY:
      status = 400;
      message = '用户名或密码不能为空。'
      break;
    case errorType.USER_IS_EXISTENCE:
      status = 401;
      message = '用户名已存在。'
      break;
    case errorType.USER_NON_EXISTENCE:
      status = 401;
      message = '用户名不存在。'
      break;
    case errorType.PASSWORD_IS_ERROR:
      status = 401;
      message = '密码错误。'
      break;
    case errorType.TOKEN_ERROR:
      status = 401;
      message = 'Token错误。'
      break;
    case errorType.UNAUTHORIZATION:
      status = 401;
      message = '无效的token。'
      break;
    case errorType.NO_PERMISSION:
      status = 401;
      message = '当前用户不具备相关权限。'
      break;
  }

  ctx.status = status;
  ctx.body = message;
}

module.exports = errorHandle;